{include file='common/head' /}
<style>
    #shareImg,#shareImg,#coverImg {
        padding:3px;
        border:1px solid #999;
        border-radius: 4px;
        max-width: 400px;
    }
</style>

<!-- 表情开始 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- Begin emoji-picker Stylesheets -->
<link href="__PLURING__/emoji/lib/css/nanoscroller.css" rel="stylesheet">
<link href="__PLURING__/emoji/lib/css/emoji.css" rel="stylesheet">
<!-- End emoji-picker Stylesheets -->
<!-- 表情结束 -->

<script type="text/javascript" charset="utf-8" src="__UEDITOR__/lang/zh-cn/zh-cn.js"></script>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <input type="hidden" name="id" value="{$data.id}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            标题
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" required="" autocomplete="off" class="layui-input" value="{$data.name|default=''}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            跳转地址
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="page" required="" autocomplete="off" class="layui-input" value="{$data.page|default=''}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            点击图片后跳转路径
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            图片
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="share_img_btn"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" id="share_img_file">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                        </div>
                        <div>
                            <img id="shareImg" src="{$data.img|default=''}" alt="请选择图片" />
                            <input type="hidden" name="img" value="{$data.img|default=''}" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            是否展示
                        </label>
                        <div class="layui-col-md6">
                            <input type="radio" name="status" value="1" title="是" {if condition="$data['status'] eq 1"}checked{/if}>
                            <input type="radio" name="status" value="2" title="否" {if condition="$data['status'] eq 2"}checked{/if}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">确定保存</button>
                    </div>
                </form>
            </div>
        </div>
        <script>
            layui.use(['form', 'layer','jquery','laydate'],function() {
                $ = layui.jquery;
                var form = layui.form,layer = layui.layer;
                var laydate = layui.laydate;

                //监听提交
                form.on('submit(add)', function(data) {
                    var num = $('[lay-submit]').index($(this));
                    var formData = new FormData($('form')[num]);
                    // 更新时间戳
                    //formData.set('hours',Math.floor(new Date($("input[name=hours]").val()).getTime() / 1000))
                    //发异步，把数据提交给php
                    $.ajax({
                        url:"{:url('Product/functionMenuEdit')}",
                        type: "post",
                        data:formData,
                        dataType:"json",
                        cache: false,                      // 不缓存
                        processData: false,                // jQuery不要去处理发送的数据
                        contentType: false,                // jQuery不要去设置Content-Type请求头
                        success:function (res) {
                            if(res.code == 200){
                                layer.alert(res.msg, {icon: 1},function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                            }else{
                                layer.alert(res.msg, {icon:2,time:1000});
                            }
                        },
                        error:function(e){
                            layer.alert("网络错误", {icon:5,time:1000});
                        },
                    });
                    return false;
                });

                //图片阅览
                $('#test1').click(function(){
                    $('#test1s').click();
                })
                $('#test1s').change(function(e){
                    var imgBox = e.target;
                    var img = getObjectURL(imgBox.files[0]);
                    $('#img').attr('src',img);
                });

                //建立一個可存取到該file的url
                function getObjectURL(file) {
                    var url;
                    if (window.createObjectURL != undefined) { // basic
                        url = window.createObjectURL(file);
                    } else if (window.URL != undefined) { // mozilla(firefox)
                        url = window.URL.createObjectURL(file);
                    } else if (window.webkitURL != undefined) { // webkit or chrome
                        url = window.webkitURL.createObjectURL(file);
                    }
                    return url;
                }

                // 绑定 分享图片
                $("#share_img_btn").click(function(){
                    $("#share_img_file").click();
                })

                // 操作成功后的回调 - 活动封面
                $('#share_img_file').change(function(e){
                    var imgBox = e.target;
                    if(imgBox.files[0].size > 1024 * 1024 *2){
                        return layer.msg('失败，文件不得超过2mb',{icon: 5})
                    }
                    var img = getObjectURL(imgBox.files[0]);
                    $('#shareImg').attr('src',img);
                    var data = {image: imgBox.files[0]}
                    var formData = new FormData();
                    formData.append('image', imgBox.files[0]);
                    formData.append('prefix','homeDisplay');
                    $.ajax({
                        url:"{:url('admin/uploads/cosFiles')}",
                        type: 'post',
                        async: false,
                        data: formData,
                        dataType: 'json',
                        processData: false,// 告诉jQuery不要去处理发送的数据
                        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                        beforeSend: function (){ //过程...
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('加载成功', {icon: 6})
                                //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                $('input[name=img]').val(res.data.url)
                            } else {
                                layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                            }
                        },
                        error:function(){
                            layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                        }
                    })
                });

            });
        </script>
{include file='common/scroll' /}        
{include file='common/footer' /}